import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pplive/common/api_service.dart';
import 'package:pplive/common/utils.dart';
import 'package:pplive/models/extensions/hot_extensions.dart';
import 'package:pplive/models/hot_model.dart';
import 'package:pplive/models/resp_model.dart';


class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _HomeState();
}

class _HomeState extends State<HomePage> with SingleTickerProviderStateMixin {
  late ThemeData theme;
  late TabController _tabController;
  List<String> tabTitles = [
    "热门",
  ];
  ValueNotifier<int> selectedIndex = ValueNotifier(0);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    selectedIndex = ValueNotifier(0);
    _tabController =
        TabController(vsync: this, initialIndex: 0, length: tabTitles.length);
    _tabController.addListener(() {
      setState(() {});
    });
  }

  Future<RespModel<HotModel>?> fetchData() async {
    ApiService apiService = ApiService();
    try {
      RespModel<HotModel> ret = await apiService.post<HotModel>(
          "/m/list/hot/v3",
          params: {"limit": "20", "offset": "0"}) as RespModel<HotModel>;
      return ret;
    } catch (e) {
      print("$e");
      return null;
    }
  }

  @override
  void dispose() {
    _tabController.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    theme = Theme.of(context);
    // TODO: implement build
    return Scaffold(
      backgroundColor: theme.colorScheme.onSurface,
      appBar: AppBar(
          scrolledUnderElevation: 0,
          toolbarHeight: 0,
          title: Text("Home"),
          bottom: PreferredSize(
              preferredSize: Size.fromHeight(48),
              child: Container(
                  child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Expanded(
                      child: TabBar(
                          tabAlignment: TabAlignment.start,
                          isScrollable: true,
                          controller: _tabController,
                          tabs: tabTitles
                              .indexedMap((index, titile) => Tab(text: titile))
                              .toList())),
                  // Container(
                  //   // width: 116,
                  //   child: Row(
                  //     children: [
                  //       IconButton(
                  //         icon: const Icon(
                  //           Icons.insert_chart_outlined_sharp,
                  //           size: 22,
                  //         ),
                  //         onPressed: () {},
                  //       ),
                  //       IconButton(
                  //         icon: const Icon(
                  //           Icons.add,
                  //           size: 22,
                  //         ),
                  //         onPressed: () {},
                  //       ),
                  //       IconButton(
                  //         icon: const Icon(
                  //           Icons.search,
                  //           size: 22,
                  //         ),
                  //         onPressed: () {},
                  //       ),
                  //     ],
                  //   ),
                  // )
                ],
              )))),
      body: SafeArea(
          child: FutureBuilder(
              future: fetchData(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  if (snapshot.data == null || snapshot.data?.data == null) {
                    return Center(
                      child: Icon(Icons.error),
                    );
                  }
                  HotModel hotModel = snapshot.data!.data!;
                  return Container(
                      color: Colors.black,
                      child: CustomScrollView(
                        slivers: hotModel.createViews(),
                      ));
                } else {
                  return Center(child: CircularProgressIndicator());
                }
              })),
    );
  }
}
